package com.ds.lens.data.common.client;

import com.ds.lens.data.common.client.vo.*;
import com.ds.lens.data.common.document.ForecastData;
import com.ds.lens.data.common.document.MetricState;
import com.ds.lens.data.common.document.datasource.LensDatasource;
import com.ds.lens.data.common.util.HttpServiceClient;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

@Slf4j
public class LensMonitorClientWraper implements LensMonitorClient {
    private LensMonitorClient monitorClient;
    private static final String message = "error occured.";

    private <T> LensResponseData<T> createErrorResponse(Exception e) {
        log.error(message, e);
        LensResponseData resp = new LensResponseData<>();
        resp.setCode(1);
        resp.setMessage(e.getMessage());
        return resp;
    }

    public LensMonitorClientWraper(String monitorUrl) {
        monitorClient = HttpServiceClient.getRestClient(LensMonitorClient.class, monitorUrl);
    }

    @Override
    public LensResponseData<LensApplicationOutVO> getApplicationByKey(String appKey) {
        try {
            return monitorClient.getApplicationByKey(appKey);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<List<LensApplicationNameOutVO>> listAllApplications() {
        try {
            return monitorClient.listAllApplications();
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<List<LensApplicationNameOutVO>> listUpdatedApplications(Long lastModifyTime) {
        try {
            return monitorClient.listUpdatedApplications(lastModifyTime);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<List<MonitorKeyOutVO>> listAvailableMonitorState() {
        try {
            return monitorClient.listAvailableMonitorState();
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<List<MonitorKeyOutVO>> listUpdatedMonitorState(Long lastModifyTime) {
        try {
            return monitorClient.listUpdatedMonitorState(lastModifyTime);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<LensMonitorDetailsOutVO> getMonitorDefinition(Long id) {
        try {
            return monitorClient.getMonitorDefinition(id);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<List<LensMonitorDetailsOutVO>> getMonitorDetailsByTags(LensMonitorTagsSearchVO tagsSearchVO) {
        try {
            return monitorClient.getMonitorDetailsByTags(tagsSearchVO);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<LensMonitorStateOutVO> getLatestMonitorEventState(Long monitorId, Integer version, String target) {
        try {
            return monitorClient.getLatestMonitorEventState(monitorId, version, target);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<Object> updateMonitorState(Long monitorId, Integer state) {
        try {
            return monitorClient.updateMonitorState(monitorId, state);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<Map<String, MetricState>> saveMonitorState(LensMonitorStateSaveInVO monitorEventState) {
        try {
            return monitorClient.saveMonitorState(monitorEventState);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<List<LensMetricsTags>> getAllMetricsTags() {
        try {
            return monitorClient.getAllMetricsTags();
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<Object> saveMetricsTags(List<LensMetricsTagsInVO> metricsTagsInVOList) {
        try {
            return monitorClient.saveMetricsTags(metricsTagsInVOList);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<LensIntegrationOutVO> getIntegrationInfo(Long id) {
        try {
            return monitorClient.getIntegrationInfo(id);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<LensMonitorChannel> getMonitorChannel(Long monitorId, Integer version, String target, Integer type) {
        try {
            return monitorClient.getMonitorChannel(monitorId, version, target, type);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<Boolean> saveMonitorChannel(LensMonitorChannelInVO channelInVO) {
        try {
            return monitorClient.saveMonitorChannel(channelInVO);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<List<Map<String, Object>>> queryMetrics(Long monitorId, Long startTime, Long endTime, Long interval) {
        try {
            return monitorClient.queryMetrics(monitorId, startTime, endTime, interval);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<List<Map<String, Object>>> queryMetrics(LensMetricInVO metricInVO) {
        try {
            return monitorClient.queryMetrics(metricInVO);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<Boolean> startExternalEvent(LensExternalEventInVO inVO) {
        try {
            return monitorClient.startExternalEvent(inVO);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<Boolean> completeExternalEvent(LensExternalEventInVO inVO) {
        try {
            return monitorClient.completeExternalEvent(inVO);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<List<ForecastData>> queryForecastMetrics(Long monitorId, Integer version, Long startTime, Long endTime) {
        try {
            return monitorClient.queryForecastMetrics(monitorId, version, startTime, endTime);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

    @Override
    public LensResponseData<LensDatasource> queryDatasource(Long id) {
        try {
            return monitorClient.queryDatasource(id);
        } catch (Exception e) {
            return createErrorResponse(e);
        }
    }

}
